package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CustomerDto;
import com.qk.entity.Customer;
import com.qk.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/customers")
@Slf4j
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @GetMapping
    public Result list(CustomerDto customerDto){
        log.info("列表查询:{}" , customerDto);
      PageResult<Customer> pageResult = customerService.listCustomer(customerDto);
       return Result.success(pageResult);
    }

    /*添加客户*/
    @PostMapping
    public Result add(@RequestBody Customer customer){
        log.info("添加客户: {} " , customer);
        customer.setUpdateTime(LocalDateTime.now());
        customer.setCreateTime(LocalDateTime.now());
        customerService.save(customer);
        return Result.success();
    }

    /*查询回显*/
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询回显: id={} " , id);
        Customer customer = customerService.getById(id);
        return Result.success(customer);
    }

    /*修改客户*/
    @PutMapping
    public Result update(@RequestBody Customer customer){
        log.info("修改客户: {}" , customer);
        customer.setUpdateTime(LocalDateTime.now());
        customerService.updateById(customer);
        return Result.success() ;
    }

    /*删除客户*/
    @DeleteMapping("/{id}")
    public Result deleteCustomer(@PathVariable Integer id) {
        customerService.removeById(id);
        return Result.success();
    }/**/

}
